package com.example.admin.controller.InterviewRecord;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.airobotinterviewrecord.entity.InterviewSchedule;
import com.example.airobotinterviewrecord.service.IInterviewScheduleService;
import com.example.airobotinterviewrecord.util.Result;
import com.example.question.entity.QuestionSet;
import com.example.question.service.QuestionSetService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/interview-schedule")
@CrossOrigin
public class InterviewScheduleController {
    private static final Logger logger = LoggerFactory.getLogger(InterviewScheduleController.class);

    @Autowired
    private QuestionSetService questionSetService;

    @Autowired
    private IInterviewScheduleService scheduleService;

    @GetMapping("/question-set/list")
    public ResponseEntity<Result<List<QuestionSet>>> listQuestionSets() {
        try {
            logger.info("开始获取题目集列表");
            List<QuestionSet> questionSets = questionSetService.list(new QueryWrapper<QuestionSet>()
                    .select("id", "name", "order_num", "positions_id", "status", "created_at", "updated_at"));
            logger.info("获取到题目集数量: {}", questionSets.size());
            return ResponseEntity.ok(Result.success(questionSets));
        } catch (Exception e) {
            logger.error("获取题目集列表失败", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Result.error("获取题目集列表失败"));
        }
    }

    // 获取所有面试安排（分页）
    @GetMapping("/getAllSchedules")
    public ResponseEntity<Result> getAllSchedules(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        IPage<InterviewSchedule> page = scheduleService.listSchedulesByPage(pageNum, pageSize);
        return ResponseEntity.ok(Result.pageSuccess(page));
    }

    // 新增面试安排
    @PostMapping("/add")
    public ResponseEntity<Result> add(@Valid @RequestBody InterviewSchedule schedule) {
        try {
            logger.info("开始添加面试安排: {}", schedule);
            boolean success = scheduleService.addSchedule(schedule);
            if (success) {
                logger.info("面试安排添加成功");
                return ResponseEntity.ok(Result.success("添加成功"));
            } else {
                logger.error("面试安排添加失败");
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                        .body(Result.error("添加失败"));
            }
        } catch (IllegalArgumentException e) {
            logger.error("添加面试安排参数错误: {}", e.getMessage());
            return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                    .body(Result.error(e.getMessage()));
        } catch (Exception e) {
            logger.error("添加面试安排失败: ", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Result.error("添加失败: " + e.getMessage()));
        }
    }

    //根据id查询面试安排
    @GetMapping("/getScheduleById/{id}")
    public Result getInterviewScheduleById(@PathVariable Integer id) {
        InterviewSchedule schedule = scheduleService.getById(id);
        if (schedule == null) {
            return Result.fail("面试安排不存在");
        }
        return Result.success(schedule);
    }

    // 删除面试安排
    @DeleteMapping("/delete/{id}")
    public ResponseEntity<Result> delete(@PathVariable Integer id) {
        try {
            logger.info("开始删除面试安排, ID: {}", id);
            String result = scheduleService.deleteScheduleById(id);
            return ResponseEntity.ok(Result.success(result));
        } catch (IllegalArgumentException e) {
            logger.error("删除面试安排参数错误: {}", e.getMessage());
            return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                    .body(Result.error(e.getMessage()));
        } catch (Exception e) {
            logger.error("删除面试安排失败: ", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Result.error("删除失败: " + e.getMessage()));
        }
    }

    // 更新面试安排
    @PostMapping("/update")
    public ResponseEntity<Result> update(@RequestBody InterviewSchedule schedule) {
        try {
            logger.info("开始更新面试安排: {}", schedule);
            String result = scheduleService.updateSchedule(schedule);
            return ResponseEntity.ok(Result.success(result));
        } catch (IllegalArgumentException e) {
            logger.error("更新面试安排参数错误: {}", e.getMessage());
            return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                    .body(Result.error(e.getMessage()));
        } catch (Exception e) {
            logger.error("更新面试安排失败: ", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Result.error("更新失败: " + e.getMessage()));
        }
    }
}